//
//  CheckBox.m
//  yuikemall
//
//  Created by yuike6 yuike6 on 12-2-27.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

#import "CheckBox.h"
#import "header.h"

static const CGFloat kHeight = 36.0f;

@interface CheckBox(Private)
- (UIImage *) checkBoxImageForStyle:(CheckBoxViewStyle)s
                            checked:(BOOL)isChecked;
- (CGRect) imageViewFrameForCheckBoxImage:(UIImage *)img;
- (void) updateCheckBoxImage;
@end


@implementation CheckBox
@synthesize style;
@synthesize checked;

- (void) dealloc
{
    YRelease(checkBoxImageView);
    YRelease(textLabel);
    
    [super dealloc];
}


- (id) initWithFrame:(CGRect)frame
               style:(CheckBoxViewStyle)aStyle
             checked:(BOOL)aChecked
{
    frame.size.height = kHeight;
    if (!(self = [super initWithFrame:frame])) {
        return self;
    }
    
    stateChangedSelector = nil;
    delegate = nil;
    style = aStyle;
    checked = aChecked;
    
    self.userInteractionEnabled = YES;
    self.backgroundColor = [UIColor clearColor];
    
    CGRect labelFrame = CGRectMake(32.0f, 7.0f, self.frame.size.width - 32, 20.0f);
    UILabel *l = [[UILabel alloc] initWithFrame:labelFrame];
    l.textAlignment = UITextAlignmentLeft;
    l.backgroundColor = [UIColor clearColor];
    l.font = [UIFont systemFontOfSize:15];
    l.textColor = RGB(0x2E, 0x2E, 0x2E);
    l.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    l.shadowColor = [UIColor whiteColor];
    l.shadowOffset = CGSizeMake(0, 1);
    [self addSubview:l];
    textLabel = l;
    
    UIImage *img = [self checkBoxImageForStyle:style checked:checked];
    CGRect imageViewFrame = [self imageViewFrameForCheckBoxImage:img];
    UIImageView *iv = [[UIImageView alloc] initWithFrame:imageViewFrame];
    iv.image = img;
    [self addSubview:iv];
    checkBoxImageView = iv;
    
    return self;
}


- (void) setText:(NSString *)text
{
    [textLabel setText:text];
}

- (void) setChecked:(BOOL)isChecked
{
    checked = isChecked;
    [self updateCheckBoxImage];
}

- (void) setStateChangedTarget:(id<NSObject>)target
                      selector:(SEL)selector
{
    delegate = target;
    stateChangedSelector = selector;
}
#pragma -
#pragma Touch related Methods

- (void) touchesBegan:(NSSet *)touches
            withEvent:(UIEvent *)event
{
    self.alpha = 0.8f;
    [super touchesBegan:touches withEvent:event];
}

- (void) touchesCancelled:(NSSet *)touches
                withEvent:(UIEvent *)event
{
    self.alpha = 1.0f;
    [super touchesCancelled:touches withEvent:event];
}

- (void) touchesEnded:(NSSet *)touches
            withEvent:(UIEvent *)event
{
    // restore alpha
    self.alpha = 1.0f;
    
    // check touch up inside
    if ([self superview]) {
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:[self superview]];
        CGRect validTouchArea = CGRectMake((self.frame.origin.x - 5),
                                           (self.frame.origin.y - 10),
                                           (self.frame.size.width + 5),
                                           (self.frame.size.height + 10));
        if (CGRectContainsPoint(validTouchArea, point)) {
            checked = !checked;
            [self updateCheckBoxImage];
            if (delegate && stateChangedSelector) {
                [delegate performSelector:stateChangedSelector
                               withObject:self];
            }
        }
    }
    
    [super touchesEnded:touches withEvent:event];
}

- (BOOL) canBecomeFirstResponder
{
    return YES;
}

#pragma -
#pragma Private Methods

- (UIImage *) checkBoxImageForStyle:(CheckBoxViewStyle)s
                            checked:(BOOL)isChecked
{
    NSString *suffix = isChecked ? @"on" : @"off";
    NSString *imageName = @"";
    switch (s) {
        case kCheckBoxViewStyleBox:
            imageName = @"cb_box_";
            break;
        case kCheckBoxViewStyleDark:
            imageName = @"cb_dark_";
            break;
        case kCheckBoxViewStyleGlossy:
            imageName = @"cb_glossy_";
            break;
        case kCheckBoxViewStyleGreen:
            imageName = @"cb_green_";
            break;
        case kCheckBoxViewStyleMono:
            imageName = @"cb_mono_";
            break;
        default:
            return nil;
    }
    imageName = [NSString stringWithFormat:@"%@%@", imageName, suffix];
    return [UIImage imageNamed:imageName];
}

- (CGRect) imageViewFrameForCheckBoxImage:(UIImage *)img
{
    CGFloat y = floorf((kHeight - img.size.height) / 2.0f);
    return CGRectMake(5.0f, y, img.size.width, img.size.height);
}

- (void) updateCheckBoxImage
{
    checkBoxImageView.image = [self checkBoxImageForStyle:style
                                                  checked:checked];
}


@end
